package com.voyage.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.voyage.common.constant.BaseController;
import com.voyage.common.constant.PageMO;
import com.voyage.common.constant.ResponseMO;
import com.voyage.system.entity.BorrowDO;
import com.voyage.system.service.BorrowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/borrow")
@Slf4j
@Api(tags = "借款管理")
public class BorrowController extends BaseController {


    private final BorrowService borrowService;

    @Autowired
    public BorrowController(BorrowService borrowService) {
        this.borrowService = borrowService;
    }


    @GetMapping
    @ApiOperation(value = "查询列表")
    public ResponseMO<List<BorrowDO>> list() {
        return success(borrowService.list(null));
    }

    @PostMapping
    @ApiOperation(value = "插入")
    public ResponseMO insert(@Validated @RequestBody BorrowDO borrowDO) {
        borrowService.insert(borrowDO);
        return success();
    }

    @PutMapping
    @ApiOperation(value = "更新")
    public ResponseMO update(@Validated @RequestBody BorrowDO borrowDO) {
        borrowService.update(borrowDO);
        return success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public ResponseMO delete(@RequestParam("id") Long id) {
        borrowService.delete(id);
        return success();
    }


    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResponseMO<IPage<BorrowDO>> page(@Validated @RequestBody PageMO pageMO) {
        IPage<BorrowDO> all = borrowService.page(pageMO);
        return success(all);
    }
}
